package com.shuqi.common;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.app.Fragment;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.Util;
import com.shuqi.controller.R;

/**
 * 网络错误处理
 * 
 * @author li.li
 * 
 */
public class NetErrorProvider {
    private static final NetErrorProvider instance = new NetErrorProvider();
    private static final int ANIM_TIME = 1000;
    /** 定时任务 */
    private Timer timer = new Timer();

    private NetErrorProvider() {}

    public static NetErrorProvider getInstance() {
        return instance;
    }

    public void retry(Fragment fragment, final NetErrorListener netErrorListener) {
        ImageView noNetImage = (ImageView) fragment.getView().findViewById(R.id.nonet_image);
        TextView retry = (TextView) fragment.getView().findViewById(R.id.retry);
        TextView nonetText = (TextView) fragment.getView().findViewById(R.id.nonet_text);

        doRetry(fragment.getActivity(), netErrorListener, noNetImage, retry, nonetText);
    }

    public void retry(final Activity act, final NetErrorListener netErrorListener) {
        ImageView noNetImage = (ImageView) act.findViewById(R.id.nonet_image);
        TextView retry = (TextView) act.findViewById(R.id.retry);
        TextView nonetText = (TextView) act.findViewById(R.id.nonet_text);

        doRetry(act, netErrorListener, noNetImage, retry, nonetText);

    }

    private void doRetry(final Activity act, final NetErrorListener netErrorListener,
            final ImageView noNetImage, final TextView retry, final TextView nonetText) {
        noNetImage.setImageResource(R.anim.no_net_loading);

        final AnimationDrawable anim = (AnimationDrawable) noNetImage.getDrawable();

        if (Util.isNetworkConnected(act)) {// 有网直接加载
            retry.setClickable(true);// 重试可点击
            if (netErrorListener != null)
                netErrorListener.netErrorCallback();
        } else {// 无网播放动画
            timer.schedule(new TimerTask() {

                @Override
                public void run() {

                    act.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {// 执行时间
                            animStop(anim, nonetText, retry);

                            noNetImage.setImageResource(R.drawable.icon_wifi_loading_10);

                            if (Util.isNetworkConnected(act)) {
                                if (netErrorListener != null)
                                    netErrorListener.netErrorCallback();
                            }
                        }
                    });


                }
            }, ANIM_TIME);

            animStart(anim, nonetText, retry);
        }

    }

    private void animStart(AnimationDrawable anim, TextView nonetText, TextView retry) {
        if (anim != null) {
            // 执行动画
            if (anim.isRunning())
                anim.stop();

            if (nonetText != null)
                nonetText.setText("正在连接网络...");

            anim.start();
            retry.setClickable(false);// 重试不可点击

        }
    }

    private void animStop(AnimationDrawable anim, TextView nonetText, TextView retry) {
        if (anim != null) {

            if (nonetText != null)
                nonetText.setText("网络不给力,请重试");

            anim.stop();
            retry.setClickable(true);// 重试可点击
        }
    }



    public interface NetErrorListener {
        public void netErrorCallback();
    }
}
